#include "leadwidget.h"

LeadWidget::LeadWidget(QWidget *parent) : QWidget(parent)
{
    m_styleSettings = new QGSettings("org.ukui.style");
    setWindowFlags( Qt::Tool);
    //添加窗管协议
    MotifWmHints hints;
    hints.flags = MWM_HINTS_FUNCTIONS|MWM_HINTS_DECORATIONS;
    hints.functions = MWM_FUNC_ALL;
    hints.decorations = MWM_DECOR_BORDER;
    XAtomHelper::getInstance()->setWindowMotifHint(this->winId(), hints);

    //大小及位置 左8 下56
    setFixedHeight(140);
    //布局
    m_closeBtn = new QPushButton(this);
    m_closeBtn->setFixedSize(QSize(16,16)); //Fix 大小设置
    m_closeBtn->setIconSize(QSize(16,16));
    m_closeBtn->setIcon(QIcon(":/icon/lead_close.svg"));

    m_text = new QWidget(this);
    QVBoxLayout *m_textLyout = new QVBoxLayout(m_text);
    label1 = new QLabel();
    label1->setFixedHeight(25);
    label2 = new QLabel();
    label2->setFixedHeight(25);
    label3 = new QLabel();
    label3->setFixedHeight(25);
    label4 = new QLabel();
    label4->setFixedHeight(25);
    m_textLyout->addWidget(label1, 0, Qt::AlignLeft);
    m_textLyout->addWidget(label2, 0, Qt::AlignLeft);
    m_textLyout->addWidget(label3, 0, Qt::AlignLeft);
    m_textLyout->addWidget(label4, 0, Qt::AlignLeft);
    m_textLyout->setSpacing(5);
    m_textLyout->setContentsMargins(0,0,0,0);

    connect(m_closeBtn, &QPushButton::clicked, this, &LeadWidget::slotCloseBtn);
    initGsetting();
    setFont();
    connect(m_styleSettings,&QGSettings::changed,this,[=](const QString &key) {
        if (key == "systemFontSize" || key == "systemFont") {
            setFont();
       }
    });
}

LeadWidget::~LeadWidget()
{

}

void LeadWidget::slotCloseBtn()
{
    this->hide();
}

void LeadWidget::initGsetting()
{
    connect(kdk::kabase::Gsettings::getPoint(), &kdk::kabase::Gsettings::systemThemeChange, this, [=]() {
        changeStyle();
    });
    changeStyle();
}

void LeadWidget::changeStyle()
{
    QString nowThemeStyle = kdk::kabase::Gsettings::getSystemTheme().toString();
    if ("ukui-dark" == nowThemeStyle || "ukui-black" == nowThemeStyle) {
        QPalette palette;
        palette.setColor(QPalette::WindowText, Qt::white);
        label1->setPalette(palette);
        label2->setPalette(palette);
        label3->setPalette(palette);
        label4->setPalette(palette);
    }else{
        QPalette palette;
        palette.setColor(QPalette::WindowText, Qt::black);
        label1->setPalette(palette);
        label2->setPalette(palette);
        label3->setPalette(palette);
        label4->setPalette(palette);
    }
}

void LeadWidget::setFont()
{
    QString fontFamily = m_styleSettings->get("systemFont").toString();
    double fontSize = m_styleSettings->get("systemFontSize").toDouble();
    QFont font(fontFamily);
    font.setPointSizeF(fontSize);
    label1->setFont(font);
    label1->setText(tr("Full-screen:click desktop"));
    label2->setFont(font);
    label2->setText(tr("Window:click window"));
    label3->setFont(font);
    label3->setText(tr("Selection:drag and drop"));
    label4->setFont(font);
    label4->setText(tr("Done:Alt+S"));
    QFontMetrics fm_label1(label1->font());
    int label1Width = fm_label1.width(label1->text());
    QFontMetrics fm_label2(label2->font());
    int label2Width = fm_label2.width(label2->text());
    QFontMetrics fm_label3(label3->font());
    int label3Width = fm_label3.width(label3->text());
    QFontMetrics fm_label4(label4->font());
    int label4Width = fm_label4.width(label4->text());
    int width = std::max(label1Width, std::max(label2Width, std::max(label3Width, label4Width)));
    label1->setFixedWidth(width);
    label2->setFixedWidth(width);
    label3->setFixedWidth(width);
    label4->setFixedWidth(width);
    m_text->setFixedWidth(width);
    this->setFixedWidth(width + 48);
    m_closeBtn->move(this->width() - 22, 8);
    m_text->move(24,20);
}
